//
//  ZBMeInputViewController.m
//  ZhiBo
//
//  Created by huangtie on 2017/11/5.
//  Copyright © 2017年 huangtie. All rights reserved.
//

#import "ZBMeInputViewController.h"

@interface ZBMeInputViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic , strong) UITableView *tableView;

@property (nonatomic , strong) UITextField *inputTextField;

@property(nonatomic, copy) void (^callBackBlock)(NSString *text);

@end

@implementation ZBMeInputViewController

+ (ZBMeInputViewController *)launch:(UIViewController *)controller
                         defultText:(NSString *)text
                           callback:(void (^)(NSString *text))callback
{
    ZBMeInputViewController *inputController = [[ZBMeInputViewController alloc] init];
    inputController.callBackBlock = callback;
    inputController.inputTextField = [[UITextField alloc] init];
    inputController.inputTextField.width = SCREEN_WIDTH - 40;
    inputController.inputTextField.height = 35;
    inputController.inputTextField.font = FONT_NORMAL_14;
    inputController.inputTextField.textColor = UICOLOR_ARGB(0xff666666);
    inputController.inputTextField.text = text;
    if (controller) {
        [controller.navigationController pushViewController:inputController animated:YES];
    }
    return inputController;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"编辑";
    
    self.navigationItem.rightBarButtonItem = [SMHelper customBarItemWithTitle:@"保存" target:self action:@selector(rightItemClick:)];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self.inputTextField becomeFirstResponder];
}

- (void)rightItemClick:(id)sender
{
    if (self.callBackBlock) {
        self.callBackBlock(self.inputTextField.text);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark DELEGATE-TABLEVIEW
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    cell.size = CGSizeMake(SCREEN_WIDTH, 50);
    self.inputTextField.centerY = cell.height / 2;
    self.inputTextField.centerX = cell.width / 2;
    [cell addSubview:self.inputTextField];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
}

@end
